﻿using OrderControl;
using System.Windows.Forms;

namespace OrderControlWinForm {
    public partial class Form_Detail : Form {
        private string stringCache;
        private decimal decimalCache;

        private OrderDetail detail;
        public OrderDetail Detail {
            get => detail;
            set {
                detail = value;
                bindingSource_Detail.DataSource = detail;
            }
        }

        public Form_Detail() {
            InitializeComponent();
            this.Detail = new OrderDetail(new Commodity("Null", 1), 1);
        }

        public Form_Detail(OrderDetail detail) : this() {
            this.Detail.Commodity.Name = detail.Commodity.Name;
            this.Detail.Commodity.UnitPrice = detail.Commodity.UnitPrice;
            this.Detail.Amount = detail.Amount;
            textBox_CommodityName.ReadOnly = true;
            numericUpDown_CommodityUnitPrice.Maximum = (decimal)detail.Commodity.UnitPrice;
            numericUpDown_CommodityUnitPrice.Minimum = (decimal)detail.Commodity.UnitPrice;
        }

        private void textBox_CommodityName_Enter(object sender, System.EventArgs e) {
            if (sender is TextBox textBox) {
                stringCache = textBox.Text;
            }
        }

        private void textBox_CommodityName_TextChanged(object sender, System.EventArgs e) {
            if (sender is TextBox textBox) {
                if (string.IsNullOrWhiteSpace(textBox.Text)) {
                    textBox.Text = stringCache;
                    MessageBox.Show("Text Cannot be null!");
                } else {
                    stringCache = textBox.Text;
                }
            }
        }

        private void button_Ok_Click(object sender, System.EventArgs e) {
            this.DialogResult = DialogResult.OK;
        }

        private void button_Cancel_Click(object sender, System.EventArgs e) {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
